﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using MsSpatialTools;
namespace QueryVisualizerTools
{
    public partial class DBCFrm : Form
    {
        public DBCFrm()
        {
            InitializeComponent();
        }

        private void DBCFrm_Load(object sender, EventArgs e)
        {
            this.btnOK.Enabled = false;
            init();
        }
        

        private void rbLoginServer_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rbLoginServer.Checked)
            {
                this.txtLoginName.Enabled = this.txtLoginPwd.Enabled = true;  
            }
        }

        private void rbLoginWindows_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rbLoginWindows.Checked)
            {
                this.txtLoginName.Enabled = this.txtLoginPwd.Enabled = false;
            }
        }

        private void cbbServerName_TextChanged(object sender, EventArgs e)
        {
            this.btnOK.Enabled = false;
        }

        private void init()
        {
            this.cbbServerName.Items.Add("ldk114\\SQLExpress");
            this.cbbServerName.Items.Add("127.0.0.1");
            this.cbbServerName.Items.Add("Administrator\\SQLExpress");
            this.cbbServerName.SelectedIndex = 0;

            this.txtLoginName.Enabled = this.txtLoginPwd.Enabled = false;
        }
       
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            QueryFrm qf = new QueryFrm();
            qf.Show();
            this.Hide();
        }

        private void btnTestCon_Click(object sender, EventArgs e)
        {
            DBHelper.SqlServer = this.cbbServerName.Text;
            DBHelper.Database = "master";
            DBHelper.Trusted_Connection = this.rbLoginWindows.Checked;
            DBHelper.UserID = this.txtLoginName.Text;
            DBHelper.Password = this.txtLoginPwd.Text;
            if (DBHelper.testConnection())
            {
                MessageBox.Show("成功！");
                this.btnOK.Enabled = true;
            }
            else
            {
                MessageBox.Show("链接失败！");
                this.btnOK.Enabled = false;
            }
        }

    }
}
